﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
namespace OnlineManagement.Web.userControl
{
    public class BaseUserControl:System.Web.UI.UserControl
    {
        private int _hoggeryId = 0;
        /// <summary>
        /// 获取猪类型
        /// </summary>
        public System.Data.DataSet GetAllPigType
        {
            get
            {
                try
                {
                    BLL.Dal.PigBreed BllPb = new BLL.Dal.PigBreed();
                    System.Data.DataSet ds = null;
                    object obj= Common.DataCache.GetCache("AllPigType");
                    if (obj != null && obj is System.Data.DataSet)
                    {
                        ds = obj as System.Data.DataSet;
                    }
                    else {
                        ds = BllPb.GetList("");
                        Common.DataCache.SetCache("AllPigType", ds,System.Web.Caching.Cache.NoAbsoluteExpiration,TimeSpan.FromMinutes(5));
                    }
                    return ds;
                }catch(Exception ex){
                  log4net.ILog log=  OnlineHoggeryLog.GetLog("BaseUserControl.GetAllPigType");
                  log.Error(new Com.HoggeryLog(), ex);
                  return null;
                }
            }
        }

        public int HoggeryID
        {
            get
            {
                try
                {
                    if (HttpContext.Current.User.Identity != null &&
                       !HttpContext.Current.User.Identity.IsAuthenticated)
                    {
                        return 0;
                    }
                    FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
                    string hoggeryIDStr = id.Ticket.UserData;

                    if (string.IsNullOrEmpty(hoggeryIDStr))
                    {
                        BLL.TempRegist loginDll = new BLL.TempRegist();
                        _hoggeryId = loginDll.GetHoggeryIdForLogin(HttpContext.Current.User.Identity.Name);
                        Common.PubSessions.CacheHoggeryID = _hoggeryId.ToString();
                    }
                    int.TryParse(hoggeryIDStr, out _hoggeryId);
                    
                }catch(Exception ex){
                }
                return _hoggeryId;
            }
        }

        public string HoggeryName {
            get
            {
                try
                {
                    if(HttpContext.Current.User.Identity!=null&&
                        !HttpContext.Current.User.Identity.IsAuthenticated){
                            return string.Empty;
                    }
                    string hoggeryName = Common.PubSessions.CacheHoggeryName;
                    if (string.IsNullOrEmpty(hoggeryName))
                    {
                        BLL.Dal.Hoggery bllHog = new BLL.Dal.Hoggery();
                        if (_hoggeryId == 0)
                        {
                            _hoggeryId = HoggeryID;
                        }
                        if (_hoggeryId != 0)
                        {
                            hoggeryName = bllHog.GetHoggeryName(_hoggeryId);
                            Common.PubSessions.CacheHoggeryName = hoggeryName;
                        }
                    }
                    return hoggeryName;
                }catch(Exception ex){
                    log4net.ILog log = OnlineHoggeryLog.GetLog("BaseUserControl.HoggeryName");
                    log.Error(new Com.HoggeryLog(), ex);
                    return string.Empty;
                }
            }
        }
    }
}